<?php

namespace app\shop\controller\Business;

use think\Controller;


class Base extends Controller
{
    // 客户模型
    protected $BusinessModel = null;
    protected $CollectionModel = null;
    protected $ProductModel = null;

    public function __construct()
    {
        // 手动继承父级的方法
        parent::__construct();

        // 加载模型
        $this->BusinessModel = model('Business.Business');

        $this->CollectionModel = model('Business.Collection');

        $this->ProductModel = model('Product.Product');
    }

    // 注册
    public function register()
    {
        $mobile = $this->request->param('mobile', '', 'trim');
        $password = $this->request->param('password', '', 'trim');

        // 客户来源
        $sourceid = model('Business.Source')->where(['name' => '商城'])->value('id');

        // 生成密码盐
        $salt = build_ranstr();

        // 进行加密
        $password = md5($password . $salt);

        // 组装数据
        $data = [
            'mobile' => $mobile,
            'password' => $password,
            'salt' => $salt,
            'sourceid' => $sourceid,
            'invitecode' => build_ranstr(),
        ];

        $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

        if ($result === FALSE) {
            $this->error($this->BusinessModel->getError());
        } else {
            $this->success('注册成功');
        }
    }

    // 登录
    public function login()
    {
        $mobile = $this->request->param('mobile', '', 'trim');
        $password = $this->request->param('password', '', 'trim');

        // 通过手机号查询数据库是否有该客户
        $Business = $this->BusinessModel->where(['mobile' => $mobile])->find();

        if (!$Business) {
            $this->error('手机号未注册，请先注册');
        }

        $password = md5($password . $Business['salt']);

        if ($password != $Business['password']) {
            $this->error('密码错误，请重新输入');
        }

        // 封装数据
        $data = [
            'id' => $Business['id'],
            'mobile' => $Business['mobile'],
            'nickname' => $Business['nickname'],
            // 前端地址跟后台地址不一样，资源在后台，如果没有带后台域名是找不到
            'avatar_cdn' => $Business['avatar_cdn'],
            'gender' => $Business['gender'],
            'province' => $Business['province'],
            'city' => $Business['city'],
            'district' => $Business['district'],
            'email' => $Business['email'],
            'auth' => $Business['auth']
        ];

        $this->success('登录成功', null, $data);
    }

    public function check()
    {
        $mobile = $this->request->param('mobile', '', 'trim');
        $busid = $this->request->param('busid', 0, 'trim');

        // 组装条件
        $where = [
            'mobile' => $mobile,
            'id' => $busid
        ];

        $Business = $this->BusinessModel->where($where)->find();

        if (!$Business) {
            $this->error('非法登录');
        }

        // 封装数据
        $data = [
            'id' => $Business['id'],
            'mobile' => $Business['mobile'],
            'mobile_text' => $Business['mobile_text'],
            'nickname' => $Business['nickname'],
            // 前端地址跟后台地址不一样，资源在后台，如果没有带后台域名是找不到
            'avatar' => $Business['avatar'],
            'avatar_cdn' => $Business['avatar_cdn'],
            'gender' => $Business['gender'],
            'province' => $Business['province'],
            'city' => $Business['city'],
            'district' => $Business['district'],
            'region_text' => $Business['region_text'],
            'email' => $Business['email'],
            'auth' => $Business['auth']
        ];

        $this->success('验证成功', null, $data);
    }

    // 修改资料
    public function profile()
    {
        // 接收全部参数
        $params = $this->request->param();

        // 通过接收的id查询数据库是否存在该用户
        $Business = $this->BusinessModel->find($params['id']);

        if (!$Business) {
            $this->error('用户不存在');
        }

        // 组装数据
        $data = [
            'id' => $params['id'],
            'nickname' => $params['nickname'],
            'gender' => $params['gender']
        ];

        // 每次修改邮箱时都重新认证
        if ($params['email'] != $Business['email']) {
            $data['email'] = $params['email'];
            $data['auth'] = 0;
        }

        // 地区
        if (!empty($params['district'])) {
            $parentpath = model('Region')->where(['code' => $params['district']])->value('parentpath');

            if (!$parentpath) {
                $this->error('选择地区不存在');
            }

            [$province, $city, $district] = explode(',', $parentpath);

            $data['province'] = $province;

            $data['city'] = $city;

            $data['district'] = $district;
        }

        // 密码
        if (isset($params['password'])) {
            $salt = build_ranstr();

            $password = md5($params['password'] . $salt);

            $data['salt'] = $salt;

            $data['password'] = $password;
        }

        // 头像
        if (isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
            $avatar = build_upload('avatar');

            if ($avatar['code'] === 0) {
                $this->error($avatar['msg']);
            }

            $data['avatar'] = $avatar['data'];
        }

        $result = $this->BusinessModel->isUpdate(true)->save($data);

        if ($result === FALSE) {
            if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                @is_file('.' . $data['avatar']) && @unlink('.' . $data['avatar']);
            }

            $this->error('更新资料失败');
        } else {
            if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                @is_file('.' . $Business['avatar']) && @unlink('.' . $Business['avatar']);
            }

            $this->success('更新资料成功');
        }
    }

    // 我的收藏页面
    public function collection()
    {
        $result = $this->CollectionModel->with(['product'])->order('createtime', 'desc')->select();

        if ($result) {

            // $list = $this->CollectionModel->join('LEFT JOIN pre_product ON pre_busniess_collection.proid = pre_product.id');
            // $list = $this->CollectionModel->with(['product'])->order('createtime', 'desc')->select();

            // halt($this->CollectionModel->getLastSql());
            $this->success('查询成功', null, $result);
        } else {

            $this->error('暂无数据', null);
        }
    }
}
